﻿using System.Windows;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using System;
using Baosight.ZNH.ZMCommon.ChildWindows;
using System.Collections.Generic;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon.Prompt;

namespace Baosight.ZNH.ZMCS.UI.ChildWindows
{
    /// <summary>
    /// 系统模块的新增、修改、查看页面
    /// 对应后台ServiceZMCS02.java
    /// @author 严洪浩
    /// </summary>
    public partial class Engineering : EFViewCmes
    {
        public Engineering(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            _view.Args.Add("numberType", this.engineeringNoType.Value == null ? "" : this.engineeringNoType.Value.ToString());
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    this.fdIsKeyProjects.Value = "0";
                    this.fdStatus.Value = "0";
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    this.fdClass.IsEnabled = false;
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    this.uploadArea.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 校验前的工作
        /// </summary>
        /// <returns></returns>
        //public override bool ThisValidateField()
        //{
        //    string fdType = this.fdType.Value == null ? "" : this.fdType.Value.ToString();
        //    string fdClass = this.fdClass.Value == null ? "" : this.fdClass.Value.ToString().Trim();
        //    if ("0".Equals(fdType) && "打钻".Equals(fdClass))
        //    {
        //        this.fdExt1.eRegex = "^.+$";
        //        this.fdExt1.eValidateErrorPrompt = "不能为空！";
        //    }
        //    else
        //    {
        //        this.fdExt1.eRegex = null;
        //    }
        //    return true;
        //}

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }
            
            bool _validateField = ThisValidateField() & this.ValidateField();//确定前的效验

            if (!_validateField)
            {
                this.ValidateMessagePopup();//弹出提示信息
                this.TempButtonOpen(true);//确定按钮生效
                return;
            }

            if (this.upload.IsBusy)
            {
                this.ValidateMessagePopup("文件正在上传中，请稍候再提交数据！");//弹出提示信息                
                this.TempButtonOpen(true);//确定按钮生效
                return;
            }

            EIInfo eiInfo = this.BuildSubmitEIInfo("ZMCS81", Mode.ToString().ToLower(), EIConstants.ResultBlock);
            Dictionary<string, string> dic = upload.InsertFileArgs;
            EIBlock inquBlock = eiInfo.AddBlock("insertfiles");
            EIColumn dir = new EIColumn();
            dir.Name = "fdKeyId";
            dir.TypeLabel = "C";
            inquBlock.AddColumn(dir);

            dir = new EIColumn();
            dir.Name = "fdMemoryName";
            dir.TypeLabel = "C";
            inquBlock.AddColumn(dir);

            dir = new EIColumn();
            dir.Name = "fdForeignId";
            dir.TypeLabel = "C";
            inquBlock.AddColumn(dir);

            foreach (string obj in dic.Keys)
            {
                EIRow row = new EIRow();
                row.Add("fdKeyId", dic[obj]);
                row.Add("fdMemoryName", obj);
                row.Add("fdForeignId", "");
                inquBlock.AddRow(row);
            }
            base.Submit(eiInfo, DoCommitCompleted);
        }

        /// <summary>
        /// 验证失败，消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 工程所属 改变值 时给 ParentTable 赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdParentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                string types = box.SelectedItem.GetType().GetProperty("types").GetValue(box.SelectedItem, null) as string;
                if (null != types)
                {
                    switch (types)
                    {
                        case "GZM":
                            this.fdParentTable.Value = "T_CI_WORKINGFACE";
                            break;
                        case "HD":
                            this.fdParentTable.Value = "T_CI_ROADWAY";
                            break;
                        default:
                            this.fdParentTable.Value = "T_CI_OTHERDESIGN";
                            break;
                    }
                }
            }
        }

        /// <summary>
        /// 工程类别发生变化时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdClass_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            string fdType = this.fdType.Value.ToString();

            if (box.SelectedItem != null)
            {
                string classes = box.SelectedItem.GetType().GetProperty("value").GetValue(box.SelectedItem, null) as string;
                if (null != classes && "打钻".Equals(classes))
                {
                    this.fdExt1.Visibility = Visibility.Visible;
                }
                else
                {
                    this.fdExt1.Visibility = Visibility.Collapsed;
                }

                if (null != fdType)
                {
                    switch (fdType)
                    {
                        case "0":
                            this.fdUnit.Value = "吨";
                            this.fdUnit.IsReadOnly = true;
                            break;
                        case "1":
                            this.fdUnit.Value = "米";
                            this.fdUnit.IsReadOnly = true;
                            break;
                        case "2":
                            this.fdUnit.IsReadOnly = true;
                            if ("打钻".Equals(classes))
                            {
                                this.fdUnit.Value = "米";
                            }
                            else if ("抽采".Equals(classes))
                            {
                                this.fdUnit.Value = "万立方米";
                            }
                            else if ("管路安装".Equals(classes))
                            {
                                this.fdUnit.Value = "米";
                            }
                            else
                            {
                                this.fdUnit.Value = "个";
                            }
                            break;
                        default:
                            this.fdUnit.IsReadOnly = false;
                            break;
                    }
                }
            }
        }
    }
}

